﻿using Microsoft.AspNetCore.Mvc;
using Scriban.Runtime.Accessors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using ThridGroup.ERP.DTO;
using ThridGroup.ERP.DTO.JobGradeManagementDTO;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace ThridGroup.ERP.RBAC
{
    /// <summary>
    /// 角色服务层
    /// </summary>
    public class RoleServices : ApplicationService, IRoleServicers
    {
        /// <summary>
        /// 角色仓储
        /// </summary>
        private readonly IRepository<Role, int> _roleServices;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="roleServices">角色仓储</param>
        public RoleServices(IRepository<Role, int> roleServices)
        {
            _roleServices = roleServices;
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <returns></returns>
        public async Task<int> AddRole(CreateRoleDto createRoleDto)
        {
            var role = ObjectMapper.Map<CreateRoleDto, Role>(createRoleDto);
            var rolecount = await _roleServices.InsertAsync(role);
            return rolecount == null ? 0 : 1;
        }

       

        /// <summary>
        /// 显示所有角色（不分页）
        /// </summary>
        /// <returns></returns>
        public async Task<List<RoleListDto>> QueryRole()
        {
            var roleList = await _roleServices.GetListAsync();
            var roleDtos = ObjectMapper.Map<List<Role>, List<RoleListDto>>(roleList);
            return roleDtos;
        }


        /// <summary>
        ///显示角色信息  分页
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<List<RoleListDto>> GetRole(int page = 1, int limit = 10)
        {
            if (page<1) 
            {
            page=1;
            }
            var roleList = await _roleServices.GetListAsync();
            var roleDtos = ObjectMapper.Map<List<Role>, List<RoleListDto>>(roleList);
            return roleDtos;

        }



        ///// <summary>
        ///// 分页显示所有角色
        ///// </summary>
        ///// <param name="skipCount">跳过多少条</param>
        ///// <param name="maxResultCount">每页多少条</param>
        ///// <returns></returns>
        //public async Task<List<RoleListDto>> QueryRole(int skipCount = 1, int maxResultCount = 10)
        //{
        //    var roleList = await _roleServices.GetPagedListAsync(skipCount, maxResultCount, null, false);
        //    var roleDtos = ObjectMapper.Map<List<Role>, List<RoleListDto>>(roleList);
        //    return roleDtos;
        //}

        /// <summary>
        /// 删除角色信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DelRole(int id) 
        {
            await _roleServices.DeleteDirectAsync(a => a.Id == id);
        }
        /// <summary>
        /// 回显角色信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<RoleListDto> FullRole(int id) 
        {
            var role = await _roleServices.FindAsync(a => a.Id == id);
            if (role == null)
            {
                return null;
            }
            return ObjectMapper.Map<Role, RoleListDto>(role);
        }

        /// <summary>
        /// 修改角色信息
        /// </summary>
        /// <param name="createRoleDto"></param>
        /// <returns></returns>
        public async Task<int> UpRole(CreateRoleDto createRoleDto) {
            var role = ObjectMapper.Map<CreateRoleDto, Role>(createRoleDto);
            var rolecount = await _roleServices.UpdateAsync(role);
            return rolecount == null ? 0 : 1;
        }
    }
}
